function getBuilding(valueDefault, is_insert) {
    var customer = "#search_customer";
    var building = "#search_building";
    var customerID = $(customer).val();
    $('#load_ajax').show();
    $.ajax({
        'url': root_url + 'Common/building',
        'dataType': 'json',
        'type': 'get',
        'data': {
            'id': customerID,
            'is_insert': is_insert
        },
        beforeSend: function (xhr) {
            $(building).attr("disabled");
        },
        'success': function (json_data) {
            var string = "";
            $.each(json_data, function (key, item) {
                var selected = "";
                if (key == valueDefault) {
                    selected = "selected='selected'";
                }
                string += "<option value='" + key + "' " + selected + ">" + item + "</option>";
            });
            $(building).html(string);
            $(building).removeAttr("disabled");
        }
    }).done(function(){
        setTimeout(function(){
            $('#load_ajax').hide();
        },2000);
        
    });
}

$(document).ready(function () {
    /**
     *
     */
    if (typeof menu_active != "undefined" && menu_active != '') {
        $('li.sub-menu').removeClass('active');
        $('#' + menu_active).parents('.sub-menu').addClass('active')
        $('#' + menu_active).parent('li').addClass('active');
    }
    $('.view_map').on('click', function () {
        var address = $(this).data('address');
        var url = root_url + "Common/map/" + address;
        var iframe = '<iframe src="' + url + '" width="760" height="400"></iframe>';
        $('#myModal .modal-body p').html(iframe);
        $('#myModal').modal('show');
        return false;
    });
//    

    /**
     * 
     */
    $('.view_camera').on('click', function () {
        var code = $(this).data('code');
        var url = root_url + "Camera/view/" + code + "/big/1";
//        var iframe = '<iframe src="' + url + '" width="520" height="300"></iframe>';
//        $('#view_camera .modal-body p').html(iframe);
//        $('#view_camera').modal('show');
        
        //location.href= url;
        params  = 'width='+screen.width;
        params += ', height='+screen.height;
        params += ', top=0, left=0'
        params += ', scrollbars=yes,resizable=yes';

        var newwin=window.open(url,'windowname4', params);
        if (window.focus) {newwin.focus()}
        return false;
    });
    //close view camera
    $('#view_camera').on('hidden', function () {
        $('#view_camera .modal-body p').html('');
    });

    /**
     * 
     */
// em comment cho doan code cu nhe
    /*$('#view_multi').on('click', function () {
        var iframe = "";
        var count = 0;
        var single = '';
        var width = 280;
        var height = 230;
        if ($('.check_view:checked').size() == 1) {
            single = '/big';
            width = 520;
            height = 300;
            $('#view_multi_camera').addClass('view_big');
            $('#view_multi_camera').removeClass('view_md');
        } else {
            //md
            if ($('.check_view:checked').size() > 1 && $('.check_view:checked').size() <= 4) {
                single = '/md';
                width = 340;
                height = 205;
                $('#view_multi_camera').removeClass('view_big');
                $('#view_multi_camera').addClass('view_md');
            } else {
                $('#view_multi_camera').removeClass('view_big');
                $('#view_multi_camera').removeClass('view_md');
            }
        }


        if ($('.check_view:checked').size() == 1) {
            $('#view_multi_camera').addClass('view_big');
        } else {
            $('#view_multi_camera').removeClass('view_big');
        }
        $('.check_view:checked').each(function (index) {
            var code = $(this).val();
            var url = root_url + "Camera/view/" + code + single;
            iframe += '<iframe src="' + url + '" width="' + width + '" height="' + height + '" style="float:left"></iframe>';
            count++;
            if (count == 16)
                return false;
        });
        if (iframe.length > 0) {
            $('#view_multi_camera .modal-body p').html(iframe);
            $('#view_multi_camera').modal('show');
        }


        return false;
    }); */
    // doan code nay la cua em
    $('#view_multi').on('click', function () {
        var data = [];
        var count = 0;
        var url = "";
        var frst_cd = 0;
        $('.check_view:checked').each(function (index) {
            data[index] = $(this).val();
            frst_cd = $(this).val();
            count++;
            if (count == 16)
                return false;
        });
        data= JSON.stringify(data);
        if(count > 1){
        	url = root_url + "Camera/multiview?data=" +encodeURIComponent(data);
        } else {
        	url = root_url + "Camera/view/"+ frst_cd +"/big/1";
        }
        
        //location.href= url;
        params  = 'width='+screen.width;
        params += ', height='+screen.height;
        params += ', top=0, left=0'
        params += ', scrollbars=yes,resizable=no';

        var newwin=window.open(url,'windowname4', params);
        if (window.focus) {newwin.focus()}
        return false;

    });

    //close view camera
//    $('#view_multi_camera').on('hidden', function () {
//        $('#view_multi_camera .modal-body p').html('');
//    });

    //
    $('.btn-danger a').on('click', function () {
        var url = $(this).attr('href');
        $.messager.confirm('Confirm', 'do you want to delete this item?', function (r) {
            if (r) {
                window.location = url;
            }
        });
        return false;
    });


    //
    $('.record_camera').on('click', function () {
        var code = $(this).data('id');
        var url = root_url + "Recording/record/" + code;
        var iframe = '<iframe src="' + url + '" width="520" height="300"></iframe>';
        $('#container_record').append(iframe);
    });

    //View Camera in google map
    $('#map_CamId').on('change', function () {
        var cust_id = $('#map_CamId option:selected').val();
        $.ajax({
            url: root_url + 'Common/get_camera',
            dataType: 'json',
            type: 'post',
            data: {
                CustId: cust_id
            },
            success: function (json_data) {
                var string = "";
                $.each(json_data, function (key, item) {

                    string += "<option value='" + key + "'>" + item + "</option>";
                });
                $('#CameraCamId').html(string);
            }
        });
    });
    // delete customer and admin
    $('.delete_customer').on('click', function () {
        $.messager.confirm('Confirm', 'do you want to delete this item?', function (r) {
            if (r) {
                return true
            }
        });
        return false;
    });

    //
    $('.btn_view_video').on('click', function () {
        var url = $(this).data('url');
        var iframe = '<iframe src="' + url + '" width="520" height="300"></iframe>';
        $('#view_video .modal-body p').html(iframe);
        $('#view_video').modal('show');
    });
    //close view camera
    $('#view_video').on('hidden', function () {
        $('#view_video .modal-body p').html('');
    });

    //
    $('.start_camera').on('click', function () {
        var code = $(this).data('id');
        var url = root_url + "Recording/startRecord/" + code;
        var $this = $(this);
        $.ajax({
            url: url,
            dataType: 'json',
            success: function (data) {
                if (data.error == 0) {
                    $this.parents('tr').find('.stop_camera').removeClass('hideField');
                    $this.addClass('hideField');
                } else {
                    alert("camera don't work");
                }
            }
        });
    });
    //
    $('.stop_camera').on('click', function () {
        var code = $(this).data('id');
        var url = root_url + "Recording/stop/" + code;
        var $this = $(this);
        $.ajax({
            url: url,
            dataType: 'json',
            success: function (data) {
                if (data.error == 0) {
                    $this.parents('tr').find('.start_camera').removeClass('hideField');
                    $this.addClass('hideField');
                } else {
                    alert('error system');
                }
            }
        });
    });

    //
    $('.view_file').on('click', function () {
        var id = $(this).data('id');
        var url = root_url + "see_file/ajax/" + id;
        var iframe = '<iframe src="' + url + '" width="940" height="400"></iframe>';
        $('#view_file .modal-body p').html(iframe);
        $('#view_file').modal('show');
        return false;
    });

    //
    if ($('#ckeditor_custom').size() > 0) {
        CKEDITOR.replace('ckeditor_custom');
    }

});




